#pragma once
#include <iostream>
#include "comm.hpp"
using namespace std;

class Init
{
public:
    Init()
    {
        int n = mkfifo(gpipefile.c_str(), gmode);
        if (n < 0)
        {
            cerr << "mkfifo error" << endl;
            return;
        }
        cout << "mkfifo success" << endl;
    }
    ~Init()
    {
        int n = unlink(gpipefile.c_str());
        if (n < 0)
        {
            cerr << "unlink error" << endl;
            return;
        }
        cout << "unlink success" << endl;
    }
};

Init it;

class Server
{
public:
    Server()
        : _fd(gdefault)
    {
    }

    ~Server()
    {
    }

    bool OpenPipeForRead()
    {
        _fd = OpenPipe(gread);
        return _fd > 0 ? true : false;
    }

    bool ClosePipe()
    {
        return ClosePipeHeap(_fd);
    }

    int ReceivePipe(string *out)
    {
        char buffer[gsize];
        ssize_t n = read(_fd, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n] = 0;
            *out = buffer;
        }
        return n;
    }

private:
    int _fd;
};